<?php
namespace app\index\controller;

use think\Session;
use think\Cookie;
use think\Controller;
use think\Request;
use think\Db;

class Screendetails extends BaseController
{
    /**
     * 在读书籍--归还申请
     */
    public function applyrollback(){
        $bid = input('post.');
        //当前登录的用户
        $userid = Session::get('userid');
        // return $bid;
        $result = db('borrow')->where('uid',$userid)->where('states=1 or states=5')->where('bid',$bid['id'])->setField('states',6);
        // dump($result);die;
        return $result;
    }

    /**
     * 申请借阅
     */
    public function doapply(){
        $userid = Session::get('userid'); 
        $data = input('post.');
        $toborrow = array();
        if($data['bookid']){
            $toborrow['bid'] = $data['bookid'];
            $toborrow['uid'] = $userid;
            $toborrow['states'] = 3;
            $toborrow['applybooktime'] = time();
//            $toborrow['booktime'] = 0;
//            $toborrow['expectedReturnTime'] = 0;
//            $toborrow['returntime'] = 0;
            $result = db('borrow') -> insert($toborrow);      //添加数据 
            return $result;
        }else{
            return 0;
        }
    }

    /**
     * 预约
     */
    public function doapplys(){
        $userid = Session::get('userid'); 
        $data = input('post.');
        $toborrow = array();
        if($data['bookid']){
            $toborrow['bid'] = $data['bookid'];
            $toborrow['uid'] = $userid;
            $toborrow['states'] = 8;
//            $toborrow['applybooktime'] = 0;
//            $toborrow['booktime'] = 0;
            $toborrow['appointmenttime'] = time();
//            $toborrow['returntime'] = 0;
            $result = db('borrow') -> insert($toborrow);      //添加数据 
            return $result;
        }else{
            return 0;
        }
    }

    /**
     * 取消申请
     */
    public function cancelapply(){
        $bid = input('post.');
        // dump($bid);die;
        //当前登录的用户
        $userid = Session::get('userid');
        //更改数据库借阅记录的状态---改为取消预约
        $result = db('borrow')->where('uid',$userid)->where('states',3)->where('bid',$bid['bookid'])->setField('states',4);
        return $result;
    }

    /**
     * 取消预约
     */
    public function cancelapplys(){
        $bid = input('post.');
        // dump($bid);die;
        //当前登录的用户
        $userid = Session::get('userid');
        //更改数据库借阅记录的状态---改为取消预约
        $result = db('borrow')->where('uid',$userid)->where('states',8)->where('bid',$bid['bookid'])->setField('states',4);
        return $result;
    }

    /**
     * 未借出图书分页
     */
    public function pageBookRecord(){
        $page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }

        $userid = Session::get('userid'); 
        //馆内书籍
        $book = db('books')->where('isdelete',1)->where('bstates',1)->select();
        //书籍是否已被本人申请
        $borrowstates = db('borrow')->where('states',3)->where('uid',$userid)->select();
        // dump($borrowstates);die;
        foreach($book as $c => $d){
            $book[$c]['type'] = 0;
            foreach($borrowstates as $a => $b){
                if($b['bid']==$d['id']){
                    $book[$c]['type'] = 1;
                }
            }
        }
        // dump($book);die;
        $query = Publicmethod::pageing($book,$page);
        if($query){
            return $query;
        }
        return 0;
    }

    /**
     * 借出图书分页
     */
    public function pageBookRecords(){
        $page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }

        $userid = Session::get('userid'); 
        //借出书籍
        $book = db('books')->where('isdelete',1)->where('bstates',2)->select();
        //借出图书
        $bookout = array();
        $m = 0;
        foreach ($book as $key => $value) {
            if($value['bstates']==2){
                $bookout[$m]['bid'] = $book[$key]['id'];
                $bookout[$m]['img'] = $book[$key]['img'];
                $bookout[$m]['bname'] = $book[$key]['bname'];
                $bookout[$m]['type'] = 0;
                //书籍是否在本人手中 0-不在，1-在
                $bookout[$m]['power'] = 0;
                $borrow = db('borrow')->where('bid',$book[$key]['id'])->select();
                foreach($borrow as $k => $val){
                    if($val['uid']==$userid){
                        if($val['states']==8){
                            //书籍是否已被本人预约
                            $bookout[$m]['type'] = 1;
                        }else if($val['states']==1){
                            $bookout[$m]['power'] = 1;
                        }else if($val['states']==5){
                            $bookout[$m]['power'] = 1;
                        }else if($val['states']==6){
                            $bookout[$m]['power'] = 1;
                            $bookout[$m]['states'] = $val['states'];
                        }
                    }
                    
                    //此时时间戳
                    $now = time();
                    if($val['states']==1|$val['states']==5|$val['states']==6){
                        $uinfo = db('user')->where('id',$val['uid'])->find();
                        $bookout[$m]['uname'] = $uinfo['name'];
                        $bookout[$m]['booktime'] = date('Y/m/d', $val['booktime']);
                        $bookout[$m]['expectedReturnTime'] = date('Y/m/d', $val['expectedReturnTime']);
                        if($val['expectedReturnTime']<$now){
                            $overtime = (int)ceil(($now-$val['expectedReturnTime'])/3600/24);
                            $bookout[$m]['overtime'] = $overtime;
                        }else{
                            $bookout[$m]['overtime'] = 0;
                        }
                    }
                }
                $m++;
            }
        }
        // dump($booksout);die;
        $query = Publicmethod::pageing($bookout,$page);
        if($query){
            return $query;
        }
        return 0;
    }

    /**
     * 馆内图书分页
     */
    public function pageBookRecordss(){
        $page = $_POST['page'];
        if(empty($page)){
            $page =1;
        }
        $userid = Session::get('userid'); 
        //搜索出的馆内书籍
        $booked = db('books')->where('isdelete',1)->where('bstates',1)->select();
        //书籍是否已被本人申请
        $borrowstates = db('borrow')->where('states',3)->where('uid',$userid)->select();
        // dump($borrowstates);die;
        foreach($booked as $c => $d){
            $booked[$c]['apply'] = 0;
            foreach($borrowstates as $a => $b){
                if($b['bid']==$d['id']){
                    $booked[$c]['apply'] = 1;
                }
            }
        }
        //借出书籍
        $book = db('books')->where('isdelete',1)->where('bstates',2)->select();
        //借出图书
        $bookout = array();
        $m = 0;
        foreach ($book as $key => $value) {
            if($value['bstates']==2){
                $bookout[$m]['bid'] = $book[$key]['id'];
                $bookout[$m]['img'] = $book[$key]['img'];
                $bookout[$m]['bname'] = $book[$key]['bname'];
                $bookout[$m]['type'] = 0;
                //书籍是否在本人手中 0-不在，1-在
                $bookout[$m]['power'] = 0;
                $borrow = db('borrow')->where('bid',$book[$key]['id'])->select();
                foreach($borrow as $k => $val){
                    if($val['uid']==$userid){
                        if($val['states']==8){
                            //书籍是否已被本人预约
                            $bookout[$m]['type'] = 1;
                        }else if($val['states']==1){
                            $bookout[$m]['power'] = 1;
                        }else if($val['states']==5){
                            $bookout[$m]['power'] = 1;
                        }else if($val['states']==6){
                            $bookout[$m]['power'] = 1;
                            $bookout[$m]['states'] = $val['states'];
                        }
                    }
                    
                    //此时时间戳
                    $now = time();
                    if($val['states']==1|$val['states']==5|$val['states']==6){
                        $uinfo = db('user')->where('id',$val['uid'])->find();
                        $bookout[$m]['uname'] = $uinfo['name'];
                        $bookout[$m]['booktime'] = date('Y/m/d', $val['booktime']);
                        $bookout[$m]['expectedReturnTime'] = date('Y/m/d', $val['expectedReturnTime']);
                        if($val['expectedReturnTime']<$now){
                            $overtime = (int)ceil(($now-$val['expectedReturnTime'])/3600/24);
                            $bookout[$m]['overtime'] = $overtime;
                        }else{
                            $bookout[$m]['overtime'] = 0;
                        }
                    }
                }
                $m++;
            }
        }
        $data = array_merge($booked,$bookout);
        $query = Publicmethod::pageing($data,$page);
        if($query){
            return $query;
        }
        return 0;
    }
}
